package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.annotation.HasPermi;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.DictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.dao.SysDictData;
import com.ruoyi.system.domain.query.SysDictDataQo;
import com.ruoyi.system.domain.view.SysDictDataVo;
import com.ruoyi.system.domain.wrapper.query.SysDictDataWrapperQ;
import com.ruoyi.system.domain.wrapper.view.SysDictDataWrapperV;
import com.ruoyi.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 字典数据Controller
 *
 * @author 尹国春
 * @date 2022-07-22
 */
@Api(value = "字典数据", tags = {"字典数据"})
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 字典数据分页查询
     */
    @ApiOperation(value="字典数据分页查询",notes="分页参数：pageNum,pageSize,orderByColumn(数据库字段名称),isAsc(asc|desc)")
    @HasPermi("system:data:list")
    @GetMapping("/list")
    public TableDataInfo<SysDictDataVo> list(SysDictDataQo sysDictData)
    {
        IPage<SysDictData> page=sysDictDataService.page(sysDictData.getPage(),SysDictDataWrapperQ.getQueryWrapper(sysDictData));
        return SysDictDataWrapperV.build().pageVO(page);
    }

    /**
     * 字典数据导出
     */
    @ApiOperation("字典数据导出")
    @HasPermi("system:data:list")
    @Log(title = "字典数据导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictDataQo sysDictData)
    {
        List<SysDictData> list = sysDictDataService.list(SysDictDataWrapperQ.getQueryWrapper(sysDictData));
        List<SysDictDataVo> voList = SysDictDataWrapperV.build().listVO(list);
        ExcelUtil<SysDictDataVo> util = new ExcelUtil<>(SysDictDataVo.class);
        util.exportExcel(response, voList, "字典数据数据");
    }

    /**
     * 获取字典数据详细信息
     */
    @ApiOperation("字典数据信息")
    @HasPermi("system:data:list")
    @GetMapping(value = "/get/{dictCode}")
    public AjaxResult getInfo(@PathVariable("dictCode") String dictCode)
    {
        SysDictData entity=sysDictDataService.getById(dictCode);
        return AjaxResult.success(SysDictDataWrapperV.build().entityVO(entity));
    }

    /**
     * 新增字典数据
     */
    @ApiOperation("字典数据新增")
    @HasPermi("system:data:edit")
    @Log(title = "新增字典数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysDictData sysDictData)
    {
        return toAjax(sysDictDataService.insert(sysDictData));
    }

    /**
     * 修改字典数据
     */
    @ApiOperation("字典数据修改")
    @HasPermi("system:data:edit")
    @Log(title = "修改字典数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SysDictData sysDictData)
    {
        return toAjax(sysDictDataService.updateById(sysDictData));
    }

    /**
     * 删除字典数据
     */
    @ApiOperation("字典数据删除")
    @HasPermi("system:data:edit")
    @Log(title = "删除字典数据", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{dictCodes}")
    public AjaxResult remove(@PathVariable String[] dictCodes)
    {
        return toAjax(sysDictDataService.removeByIds(Arrays.asList(dictCodes)));
    }
    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<DictData> data = DictUtils.getDictCache(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<DictData>();
        }
        return AjaxResult.success(data);
    }
}
